package com.handa.admin.common.config;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class StringToLongListDeserializer extends JsonDeserializer<List<Long>> {

    @Override
    public List<Long> deserialize(JsonParser p, DeserializationContext ctxt)
            throws IOException, JsonProcessingException {

        List<Long> result = new ArrayList<>();
        JsonNode node = p.getCodec().readTree(p);

        if (node.isArray()) {
            for (JsonNode element : node) {
                if (element.isTextual()) {
                    // 处理字符串数字
                    result.add(Long.valueOf(element.asText()));
                } else if (element.isNumber()) {
                    // 处理数字
                    result.add(element.asLong());
                } else if (element.isNull()) {
                    // 处理null值
                    result.add(null);
                }
            }
        }

        return result;
    }
}